package com.company;

import com.company.composite.Component;
import com.company.composite.Composite;
import com.company.composite.Leaf;

/**
 * 组合模式测试代码
 */
public class TestForComposite {

    public static void main(String[] args) {
        // 定义一个Component抽象组件对象，也可以定义多个
        Component root = new Composite("总公司");

        // 定义多个Composite组合对象
        Component c1 = new Composite("分公司A");
        Component c2 = new Composite("分公司B");
        Component c3 = new Composite("业务部");

        // 定义多个Leaf叶子对象
        Component leaf1 = new Leaf("技术部");
        Component leaf2 = new Leaf("销售部");
        Component leaf3 = new Leaf("人事部");
        Component leaf4 = new Leaf("业务部1");
        Component leaf5 = new Leaf("业务部2");

        // 组合成为树形的对象结构
        root.addChild(c1);
        root.addChild(c2);
        c1.addChild(leaf1);
        c1.addChild(leaf2);
        c1.addChild(leaf3);
        c2.addChild(leaf1);
        c2.addChild(c3);
        c3.addChild(leaf4);
        c3.addChild(leaf5);

        // 调用根对象方法打印整棵树
        root.display("");

        System.out.println("===============================");

        Component component1 = root.getChildren(0);
        System.out.println(component1.getName());
    }
}
